package com.zzt.questionbankusermanager.controller;

import com.zzt.questionbankusermanager.bean.dto.*;
import com.zzt.questionbankusermanager.bean.dto.query.MenuItemQueryDTO;
import com.zzt.questionbankusermanager.service.MenuItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menuItem")
public class MenuItemController {
    @Autowired
    private MenuItemService menuItemService;

    @RequestMapping("/getAll")
    public Result<MenuList> getAll(){
        MenuList menuList=menuItemService.getAll();
        Result<MenuList> result=new Result<>();
        result.setCode(200);
        result.setMsg("success");
        result.setData(menuList);
        return result;
    }

    @RequestMapping("/getMenuList")
    public Result<List<MenuItemDTO>> getMenuList(){
        Result<List<MenuItemDTO>> result=new Result<>();
        result.setCode(200);
        result.setMsg("success");
        result.setData(menuItemService.getMenuList());
        return result;
    }

    @RequestMapping("/getAPage")
    public PageResult<MenuItemDTO> getAPage(@RequestBody PageRequestMessage pageRequestMessage){
        if(pageRequestMessage.getPage()==null||pageRequestMessage.getPage()<2){
            pageRequestMessage.setPage(0);
        }
        else {
            pageRequestMessage.setPage(pageRequestMessage.getPage()-1);
        }
        if(pageRequestMessage.getSize()==null||pageRequestMessage.getSize()<0){
            pageRequestMessage.setSize(8);
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest pageRequest = PageRequest.of(pageRequestMessage.getPage(), pageRequestMessage.getSize(), sort);
        return menuItemService.getAPage(pageRequest);
    }

    @RequestMapping("/getASpecialPage")
    public PageResult<MenuItemDTO> getASpecialPage(@RequestBody MenuItemQueryDTO dto){
        if(dto.getPage()==null||dto.getPage()<2){
            dto.setPage(0);
        }
        else {
            dto.setPage(dto.getPage()-1);
        }
        if(dto.getSize()==null||dto.getSize()<0){
            dto.setSize(8);
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest pageRequest = PageRequest.of(dto.getPage(), dto.getSize(), sort);
        return menuItemService.getASpecialPage(dto.getWhereClause(),pageRequest);
    }

    @RequestMapping("/addMenuItem")
    public Result addMenuItem(@RequestBody MenuItemDTO menuItemDTO){
        return menuItemService.addMenuItem(menuItemDTO);
    }

    @RequestMapping("/editMenuItem")
    public Result editMenuItem(@RequestBody MenuItemDTO menuItemDTO){
        System.out.println(menuItemDTO);
        return menuItemService.editMenuItem(menuItemDTO);
    }

    @RequestMapping("/deleteMenuItem")
    public Result deleteMenuItem(@RequestBody Long menuItemId){
        return menuItemService.deleteMenuItem(menuItemId);
    }

}
